#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/5/12 02:22
# @Author  : Samge
import time
from asyncio.coroutines import iscoroutinefunction


def coast_time(func):
    """装饰器统计运行耗时"""
    def fun(*args, **kwargs):
        t = time.perf_counter()
        result = func(*args, **kwargs)
        print(f'func {func.__name__} coast time:{time.perf_counter() - t:.8f} s')
        return result

    async def func_async(*args, **kwargs):
        t = time.perf_counter()
        result = await func(*args, **kwargs)
        print(f'func_async {func.__name__} coast time:{time.perf_counter() - t:.8f} s')
        return result

    if iscoroutinefunction(func):
        return func_async
    else:
        return fun
